package com.deer.wms.instock.service.impl;

import cn.hutool.core.bean.OptionalBean;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deer.wms.ASN.manage.model.asn.AsnData;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.ASN.manage.model.in.AcceptRecord;
import com.deer.wms.ASN.manage.service.AsnDetailService;
import com.deer.wms.ASN.manage.service.AsnMasterService;
import com.deer.wms.base.system.model.Area.AreaInfo;
import com.deer.wms.base.system.model.Item.ItemInfo;
import com.deer.wms.base.system.model.Supplier.SupplierManage;
import com.deer.wms.base.system.service.AreaInfoService;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.busine.tactic.model.PrintTempInfo;
import com.deer.wms.busine.tactic.service.PrintTempInfoService;
import com.deer.wms.busine.tactic.service.PrintTempUtils;
import com.deer.wms.instock.dao.AllotMasterMapper;
import com.deer.wms.instock.model.Allot.AllotData;
import com.deer.wms.instock.model.Allot.AllotDetail;
import com.deer.wms.instock.model.Allot.AllotMaster;
import com.deer.wms.instock.model.Allot.AllotMasterCriteria;
import com.deer.wms.instock.service.AllotDetailService;
import com.deer.wms.instock.service.AllotMasterService;
import com.deer.wms.instock.service.enter.impl.Allot;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.model.Inventory.ItemBatch;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.BeanUtils;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.project.root.util.StringUtils;
import com.deer.wms.review.manage.model.BillRecord;
import com.deer.wms.review.manage.service.BillRecordService;
import com.deer.wms.review.manage.service.CaseMasterService;
import com.deer.wms.system.manage.model.DataDictDetail;
import com.deer.wms.system.manage.model.address.ShippingAddress;
import com.deer.wms.system.manage.model.carrier.Carrier;
import com.deer.wms.system.manage.service.*;
import com.deer.wms.ware.task.config.StrategyErpService;
import com.deer.wms.ware.task.dao.SoMasterMapper;
import com.deer.wms.ware.task.model.SO.*;
import com.deer.wms.ware.task.model.out.OutDetail;
import com.deer.wms.ware.task.model.pickTask.PickTask;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PickTaskService;
import com.deer.wms.ware.task.service.PushErpService;
import com.deer.wms.ware.task.service.SoDetailService;
import com.deer.wms.ware.task.service.SoMasterService;
import com.deer.wms.ware.task.service.impl.SoMasterServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.deer.wms.project.root.util.CollectionUtils.convertMap;
import static com.deer.wms.project.root.util.CollectionUtils.convertSet;

/**
 * Created by ll on 2020/03/04.
 */
@Service
@Transactional
public class AllotMasterServiceImpl extends SuperServiceImpl<AllotMasterMapper, AllotMaster> implements AllotMasterService {

    @Autowired
    private AllotMasterMapper allotMasterMapper;

    @Autowired
    private AllotMasterService allotMasterService;

    @Autowired
    private AllotDetailService allotDetailService;

    @Autowired
    private ValidatorService validatorService;

    @Autowired
    private CaseMasterService caseMasterService;

    @Autowired
    private AsnMasterService asnMasterService;

    @Autowired
    private SoMasterServiceImpl soMasterService;

    @Autowired
    private CarrierService carrierService;

    @Autowired
    private ShippingAddressService shippingAddressService;

    @Autowired
    private BillRecordService billRecordService;
    @Autowired
    private InventoryService inventoryService;

    @Autowired
   private StrategyErpService strategyErpService;

    @Override
     public List<AllotMaster> findList(AllotMasterCriteria criteria){
        return allotMasterMapper.findList(criteria);
    }

    @Autowired
    private AreaInfoService areaInfoService;
    @Autowired
    private CodeRuleService codeRuleService;

    @Override
    public void createAllotMaster(AllotData allotData, CurrentUser currentUser) {

        AllotMaster allotMaster = allotData.getAllotMaster();
        Integer fromErpAreaId = allotData.getAllotMaster().getFromErpAreaId();
        AreaInfo areaInfo = areaInfoService.getOne(new QueryWrapper<AreaInfo>().eq("erp_area_id", fromErpAreaId).last("limit 1"));
        if (ObjectUtil.isNotEmpty(areaInfo.getWaveType()) && areaInfo.getWaveType() == 2) {
            List<Inventory> invList = new ArrayList<>();
            List<AllotDetail> allotDetails = allotData.getAllotDetails();
            List<OutDetail> outDetails = new ArrayList<>();
            for (AllotDetail allotDetail : allotDetails) {
                Inventory inventory = new Inventory();
                OutDetail outDetail = new OutDetail();
                inventory.setItemCode(allotDetail.getItemCode());
                inventory.setItemName(allotDetail.getItemName());
                inventory.setBatchName(allotDetail.getBatchName());
                inventory.setErpAreaId(allotData.getAllotMaster().getToErpAreaId());
                inventory.setWareId(285);
                inventory.setWareName("如高高压");
                inventory.setCellCode(cellInfoService.findErpAreaId(allotData.getAllotMaster().getToErpAreaId()));
                inventory.setBoxCode(codeRuleService.generateCellCodeRedis());
                inventory.setOrganizationId(-1);
                inventory.setOrganizationName("自有");
                inventory.setPackDetailId(33469);
                inventory.setPackDescribe("件");
                inventory.setQuantity(allotDetail.getExpectQuantity());
                outDetail.setDeliveryQuantity(allotDetail.getExpectQuantity());
                outDetail.setErpAreaId(fromErpAreaId);
                outDetail.setBatchName(allotDetail.getBatchName());
                outDetail.setToErpId(allotData.getAllotMaster().getToErpAreaId());
                outDetail.setItemId(allotDetail.getErpItemId());
                invList.add(inventory);
                outDetails.add(outDetail);

            }
            SoMaster soMaster = new SoMaster();
            soMaster.setSoType(104);
            inventoryService.saveBatch(invList);
            String tableName = "allot_master";
            Integer nextId = validatorService.getNextId(tableName);
            String billNo = "DB" + "-" + nextId;
            Integer createUserId = currentUser.getUserId();
            String createUserName = currentUser.getUserName();
            String createTime = DateUtils.getNowDateTimeString();
            //保存调拨单表头信息
            allotMaster.setCreateUserId(createUserId);
            allotMaster.setCreateUserName(createUserName);
            allotMaster.setCreateTime(createTime);
            allotMaster.setBillNo(billNo);
            allotMaster.setCaseState(2);
            allotMaster.setInsertType(allotData.getInsertType());
            allotMasterService.save(allotMaster);
            for (int i = 0; i < allotDetails.size(); i++) {
                String detailNo = billNo + "-" + (i + 1);
                AllotDetail allotDetail = allotDetails.get(i);
                allotDetail.setBillNo(billNo);
                allotDetail.setDetailNo(detailNo);
                allotDetailService.save(allotDetail);
            }
            strategyErpService.resolveSo(soMaster, outDetails, new BaseAssist());
            return;
        }

        String tableName = "allot_master";
        Integer nextId = validatorService.getNextId(tableName);
        String billNo = "DB" + "-" + nextId;
        Integer createUserId = currentUser.getUserId();
        String createUserName = currentUser.getUserName();
        String createTime = DateUtils.getNowDateTimeString();

        //添加调拨单跟踪信息
        BillRecord billRecord = new BillRecord();
        billRecord.setBillNo(billNo);
        billRecord.setRecordType(38);//调拨新增
        billRecord.setCreateTime(createTime);
        billRecord.setCreateUserId(createUserId);
        billRecord.setCreateUserName(createUserName);
        billRecordService.createBillRecord(billRecord, currentUser);

        //状态   2-等待审核(初始化)  1-审核通过  0-审核中  -1-审核退回
        Integer state = 2;
        /**
         * 创建方式为草稿
         */
        if (allotData.getInsertType() == -2) {
            state = 2;
            BillRecord billRecord1 = new BillRecord();
            billRecord1.setBillNo(billNo);
            billRecord1.setRecordType(39);//调拨草稿
            billRecord1.setCreateTime(createTime);
            billRecord1.setCreateUserId(createUserId);
            billRecord1.setCreateUserName(createUserName);
            billRecordService.createBillRecord(billRecord1, currentUser);
        }
        /**
         * 提交
         */
        else if (allotData.getInsertType() == 0) {
            String billNo1 = allotData.getAllotMaster().getBillNo();
            //通过草稿提交，则需要先把草稿删掉
            if (billNo1 != null) {
                asnMasterService.deleteByBillNo(billNo1);
                BillRecord billRecord1 = billRecordService.findByBillNoAndType(billNo, 39);//调拨草稿
                billRecord1.setModifyTime(DateUtils.getNowDateTimeString());
                billRecord1.setModifyUserId(currentUser.getUserId());
                billRecord1.setModifyUserName(currentUser.getUserName());
                billRecordService.updateById(billRecord1);
            }
            /**
             * 添加审核流
             */
            //        -1不通过  0-流转中 1-已通过
            state = caseMasterService.createCase(billNo, 2, currentUser.getUserId(), allotMaster.getFromWareId(), allotMaster.getOrganizationId());
            BillRecord billRecord1 = new BillRecord();
            billRecord1.setBillNo(billNo);
            billRecord1.setRecordType(40);//调拨审核
            billRecord1.setCreateTime(createTime);
            billRecord1.setCreateUserId(createUserId);
            billRecord1.setCreateUserName(createUserName);
            billRecordService.createBillRecord(billRecord1, currentUser);
            if (state == 1) {
                billRecord1.setModifyTime(DateUtils.getNowDateTimeString());
                billRecord1.setModifyUserId(createUserId);
                billRecord1.setModifyUserName(createUserName);
                billRecordService.updateById(billRecord1);
            }
        }

        //保存调拨单表头信息
        allotMaster.setCreateUserId(createUserId);
        allotMaster.setCreateUserName(createUserName);
        allotMaster.setCreateTime(createTime);
        allotMaster.setBillNo(billNo);
        allotMaster.setCaseState(state);
        allotMaster.setInsertType(allotData.getInsertType());
        allotMasterService.save(allotMaster);
        //保存调拨单明细信息
        List<AllotDetail> allotDetails = allotData.getAllotDetails();
        List<Integer> invIds = allotDetails.stream().map(AllotDetail::getInventoryId).collect(Collectors.toList());
        List<Inventory> inventories = inventoryService.listByIds(invIds);
        Map<Integer, Inventory> invMap = convertMap(inventories, Inventory::getInventoryId);
        Map<Integer, List<AllotDetail>> allotMap = allotDetails.stream().collect(Collectors.groupingBy(AllotDetail::getInventoryId));
        allotMap.forEach((k,v)->{
            Inventory inventory = invMap.get(k);
            Double qty =   inventory.getQuantity() - inventory.getAllotQuantity();
            double sum = v.stream().mapToDouble(AllotDetail::getExpectQuantity).sum();
            if (sum > qty) {
                throw new ServiceException(CommonCode.REQUEST_FILE_ERROR,"分配数量大于库存数量");
            }
        });
        for (int i = 0; i < allotDetails.size(); i++) {
            String detailNo = billNo + "-" + (i + 1);
            AllotDetail allotDetail = allotDetails.get(i);
            allotDetail.setBillNo(billNo);
            allotDetail.setDetailNo(detailNo);
            allotDetailService.save(allotDetail);
        }
        BillRecord billRecord1 = billRecordService.findByBillNoAndType(billNo, 38);//调拨新增
        billRecord1.setModifyTime(DateUtils.getNowDateTimeString());
        billRecord1.setModifyUserId(createUserId);
        billRecord1.setModifyUserName(createUserName);
        billRecordService.updateById(billRecord1);

    }

    @Override
    public void deleteByBillNo(String billNo) {
        allotMasterMapper.deleteByBillNo(billNo);
    }

    @Override
    public AllotMaster findBy(String field, String value) {
        QueryWrapper<AllotMaster> qw = new QueryWrapper<>();
        return getOne(qw.eq(field, value));
    }


    @Autowired

    private PrintTempInfoService printTempInfoService;
    @Autowired
    private ItemInfoService itemInfoService;



    @Override
    public String allotPrint(String billNo, CurrentUser currentUser) {
        AllotMaster allotMaster = getOne(new QueryWrapper<AllotMaster>().eq("bill_no", billNo));
        List<AllotDetail> allotDetails = allotDetailService.list(new QueryWrapper<AllotDetail>().eq("bill_no", billNo));


        PrintTempInfo printTempInfo = printTempInfoService.findByWare(285, "快递面单");
        if (printTempInfo == null) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "未匹配到验收标签模板");
        }
        String html = printTempInfo.getTempHtml();
        Document doc = Jsoup.parse(html);
        //删除不显示的
        doc.select(".dis-none").remove();
        //直线中间蓝点的问题
        doc.select(".line-handle").remove();
//         if(inventory==null){
//            doc.select(".baokuan").remove();
//        } else if(!acceptRecord.getCellCode().equals(inventory.getCellCode())){
//             doc.select(".baokuan").remove();
//         }else  if(inventory.getBaoKuan()==0 &&  inventory.getAllotQuantity()<0.01 ){
//             doc.select(".baokuan").remove();
//         }
        doc.select(".baokuan").remove();
        Elements select = doc.select(".detail");
        Elements select1 = doc.select(".title");

        select.get(3).remove();
        select.get(4).remove();
        select1.get(2).remove();
        select1.get(3).remove();



        for (Element element : select) {
            System.out.println(element);
        }


//        String acceptInfo =
//                acceptRecord.getAcceptRecordCode() + "---" + acceptRecord.getItemCode() + "---" + acceptRecord.getItemName() + "---" + itemBatch.getBatchName() + "---" + acceptRecord.getQuantity();
        PrintTempUtils.setTempData("SHIP_BILL_NO", billNo, doc, "text");
        PrintTempUtils.setTempData("SHIP_BILL_NO_BAR_CODE", billNo, doc, "qrCode");
        PrintTempUtils.setTempData("PRINT_TIME", DateUtils.getNowDateTimeString(), doc, "text");
        PrintTempUtils.setTempData("PRINT_DATE", DateUtils.getNowDateString(), doc, "text");
        PrintTempUtils.setTempData("PRINT_USER_NAME", currentUser.getUserName(), doc, "text");


        if (doc.getElementById("FULL_ITEMS_DETAIL_TABLE") != null) {
            Element FULL_ITEMS_DETAIL_TABLE = doc.getElementById("FULL_ITEMS_DETAIL_TABLE");
            Element tbody = FULL_ITEMS_DETAIL_TABLE.getElementsByTag("tBody").get(0);
            Element ITEM_LIST_DETAIL = tbody.getElementById("ITEM_LIST_DETAIL");
            Integer colspan = 0;

            for (AllotDetail allotDetail : allotDetails) {
                Element newEle = ITEM_LIST_DETAIL.clone();
                ItemInfo itemInfo = itemInfoService.getOne(new QueryWrapper<ItemInfo>().eq("item_code", allotDetail.getItemCode()));
                colspan += PrintTempUtils.setTempData("ITEM_LIST_DETAIL_ITEM_CODE", allotDetail.getItemCode(), newEle, "text");
                colspan += PrintTempUtils.setTempData("ITEM_LIST_DETAIL_ITEM_NAME", allotDetail.getItemName(), newEle, "text");
                colspan += PrintTempUtils.setTempData("ITEM_LIST_DETAIL_MODEL", itemInfo.getModel(), newEle, "text");
                colspan += PrintTempUtils.setTempData("ITEM_LIST_DETAIL_QTY", allotDetail.getExpectQuantity(), newEle, "text");
                colspan += PrintTempUtils.setTempData("ITEM_LIST_DETAIL_FROM_AREA_NAME", allotMaster.getToAreaName(), newEle, "text");
                colspan += PrintTempUtils.setTempData("ITEM_LIST_DETAIL_TO_AREA_NAME",allotMaster.getFromAreaName() , newEle, "text");
                colspan += PrintTempUtils.setTempData("ITEM_LIST_DETAIL_REMAKE", allotDetail.getMemo(), newEle, "text");

                newEle.appendTo(tbody);
            }

            ITEM_LIST_DETAIL.remove();
        }


        //doc在创建时里面包含了html head body等节点  不需要
        Element labelContent = doc.getElementsByAttributeValue("class", "label-content").get(0);
        String tempHtml = labelContent.toString();

        return tempHtml;
    }


    @Autowired
    private SoMasterMapper soMasterMapper;
    @Autowired
    private DataDictDetailService dataDictDetailService;
    @Autowired
    private AsnDetailService asnDetailService;
    @Override
    public List<SoMasterDto> findSoMasterList(SoMasterCriteria criteria) {
        List<SoMasterDto> list = soMasterMapper.findList(criteria);
        List<DataDictDetail> dataDictDetails = dataDictDetailService.list(new QueryWrapper<DataDictDetail>().eq("data_dict_code", "else"));
        Map<String, DataDictDetail> dataMap = convertMap(dataDictDetails, DataDictDetail::getDictKey);
        List<String> orderBillNo = list.stream().map(item -> item.getOrderBillNo()).collect(Collectors.toList());
        List<AsnDetail> asnDetails = asnDetailService.list(new QueryWrapper<AsnDetail>().in("bill_no", orderBillNo));
        Map<String, AsnDetail> asnDetailMap = convertMap(asnDetails, AsnDetail::getBillNo);
        for (SoMasterDto soMasterDto : list) {
            DataDictDetail dataDictDetail = dataMap.get(Convert.toStr(soMasterDto.getCategoryId()));
            if (ObjectUtil.isNotEmpty(dataDictDetail)) {
                soMasterDto.setCategoryName(dataDictDetail.getDictValue());
            }
            AsnDetail asnDetail = asnDetailMap.get(soMasterDto.getOrderBillNo());
            if (ObjectUtil.isNotEmpty(asnDetail)) {
                soMasterDto.setMemo(asnDetail.getUda3());
                soMasterDto.setItemName(asnDetail.getItemName());
                soMasterDto.setAcceptTime(asnDetail.getAcceptTime());
            }
        }
        return list;
    }

    @Autowired
    private PickTaskService pickTaskService;
    @Autowired
    private SoDetailService soDetailService;


    @Autowired
    private CellInfoService cellInfoService;

    @Override
    public void createAsnBill(AllotData allotData, CurrentUser currentUser){
        Integer fromErpAreaId = allotData.getAllotMaster().getFromErpAreaId();
        AreaInfo area = areaInfoService.getOne(new QueryWrapper<AreaInfo>().eq("erp_area_id", fromErpAreaId).last("limit 1"));
        if (ObjectUtil.isNotEmpty(area.getWaveType()) && area.getWaveType() == 2) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "发货仓不可生产拣货任务！");
        }
        List<SoMaster> asnMasterList = soMasterService.list(new QueryWrapper<SoMaster>().eq("bill_no", allotData.getAllotMaster().getBillNo()));
        if (ObjectUtil.isNotEmpty(asnMasterList)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "该订单已分配！");
        }
        //生成ASN单
        Integer createUserId = currentUser.getUserId();
        String createUserName = currentUser.getUserName();
        String createTime = DateUtils.getNowDateTimeString();
        BillRecord billRecord =new BillRecord();
        billRecord.setBillNo(allotData.getAllotMaster().getBillNo());
        billRecord.setRecordType(41);//生成ASN
        billRecord.setCreateTime(createTime);
        billRecord.setCreateUserId(createUserId);
        billRecord.setCreateUserName(createUserName);
        billRecordService.createBillRecord(billRecord,currentUser);
        //生成SO单
        SoData soData = new SoData();
        soData.setFromAreaId(allotData.getAllotMaster().getFromErpAreaId());
        soData.setBillSource(3);//调拨生成
        soData.setInsertType(0);//提交
        BillRecord billRecord1 =new BillRecord();
        billRecord1.setBillNo(allotData.getAllotMaster().getBillNo());
        billRecord1.setRecordType(42);//生成SO
        billRecord1.setCreateTime(createTime);
        billRecord1.setCreateUserId(createUserId);
        billRecord1.setCreateUserName(createUserName);
        billRecordService.createBillRecord(billRecord1,currentUser);
        SoMaster soMaster = new SoMaster();
        BeanUtils.copyProperties(allotData.getAllotMaster(),soMaster);
//        soMaster.setBillNo(null);
        soMaster.setAllotBillNo(allotData.getAllotMaster().getBillNo());
        soMaster.setWareId(allotData.getAllotMaster().getFromWareId());
        soMaster.setWareName(allotData.getAllotMaster().getFromWareName());
        soMaster.setSoType(41);
        soData.setSoMaster(soMaster);
        List<SoDetail>soDetails = new ArrayList<>();
        for(AllotDetail allotDetail :allotData.getAllotDetails()){
            SoDetail soDetail = new SoDetail();
            BeanUtils.copyProperties(allotDetail,soDetail);
            soDetail.setMergeNo(allotDetail.getMemo());
            soDetail.setAllotDetailNo(allotDetail.getDetailNo());
            soDetail.setOrderQuantity(allotDetail.getExpectQuantity());
            soDetail.setBillNo(null);
            soDetail.setDetailNo(null);
            soDetails.add(soDetail);
        }
        soData.setSoDetails(soDetails);
        soMasterService.createSoMaster(soData, currentUser);
        AllotMaster allotMaster = allotData.getAllotMaster();
        allotMaster.setState(2);//全部调拨
        allotMasterService.updateById(allotMaster);
        List<AllotDetail> allotDetails = allotData.getAllotDetails();
        for(AllotDetail allotDetail :allotDetails){
            allotDetail.setState(2);//全部调拨
            allotDetailService.updateById(allotDetail);
        }
        Integer toErpAreaId = allotData.getAllotMaster().getToErpAreaId();
        AreaInfo areaInfo = areaInfoService.getOne(new QueryWrapper<AreaInfo>().eq("erp_area_id", toErpAreaId));
        Integer waveType = areaInfo.getWaveType();


        //领料库
        if (allotData.getAllotMaster().getToErpAreaId().equals(63662) ||2 == OptionalBean.ofNullable(waveType).orElse(0)) {

            List<SoDetail> details = soDetailService.list(new QueryWrapper<SoDetail>().eq("bill_no", allotData.getAllotMaster().getBillNo()));
            List<Integer> detailIds = details.stream().map(item -> item.getSoDetailId()).collect(Collectors.toList());
            List<PickTask> pickTasks = pickTaskService.list(new QueryWrapper<PickTask>().in("so_detail_id", detailIds));
            String pickIds = pickTasks.stream().map(item -> item.getPickTaskId().toString()).collect(Collectors.joining(","));
            for (PickTask pickTask : pickTasks) {
                //特殊情况，拣货数量直接等于计划数量
                //这里也支持部分拣货的情况
                pickTask.setReviewQuantity(pickTask.getPickQuantity());
            }
            String cellCode = cellInfoService.findErpAreaId(allotData.getAllotMaster().getToErpAreaId());
            soMasterService.allot(pickIds, cellCode);
            //扣减库存，生成出库单
            pickTaskService.pickAndSendOut(pickTasks, currentUser, allotData.getAllotMaster().getToErpAreaId());
            return;
        }


        AsnData asnData = new AsnData();
        asnData.setBillSource(4);//调拨生成
        asnData.setInsertType(0);//提交
        AsnMaster asnMaster = new AsnMaster();
        BeanUtils.copyProperties(allotData.getAllotMaster(),asnMaster);
        asnMaster.setWareId(allotData.getAllotMaster().getFromWareId());
        asnMaster.setWareName(allotData.getAllotMaster().getFromWareName());
        asnMaster.setAllotBillNo(allotData.getAllotMaster().getBillNo());
        asnMaster.setAsnType(41);
        asnData.setAsnMaster(asnMaster);
        List<AsnDetail> details = new ArrayList<>();
        for(AllotDetail allotDetail :allotData.getAllotDetails()){
            AsnDetail asnDetail = new AsnDetail();
            BeanUtils.copyProperties(allotDetail,asnDetail);
            asnDetail.setAgreementNo(allotDetail.getMemo());
            asnDetail.setAllotDetailNo(allotDetail.getDetailNo());
            asnDetail.setItemCode(allotDetail.getItemCode());
            asnDetail.setItemName(allotDetail.getItemName());
            asnDetail.setPackDetailId(allotDetail.getPackDetailId());
            asnDetail.setPackDescribe(allotDetail.getPackDescribe());
            asnDetail.setTransRatio(allotDetail.getTransRatio());
            asnDetail.setQcState(1);//合格
            asnDetail.setBillNo(null);
            asnDetail.setDetailNo(null);
            details.add(asnDetail);
        }
        asnData.setAsnDetails(details);
        if (ObjectUtil.isNotEmpty(allotData.getAllotMaster().getCustomerId())) {
            return;
        }

        if (2 == OptionalBean.ofNullable(waveType).orElse(0)) {
            return;
        }
        if (ObjectUtil.isEmpty(allotData.getAllotMaster().getCustomerId())) {
            asnMasterService.createAsnMaster(asnData,currentUser);
        }
    }
}
